#pragma once
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include "Room.h"
using namespace std;

class Building
{
private:
	vector<Room> rm;
public:
	Building(void){}
	~Building(void){}

	void Add(Room r)
	{
		rm.push_back(r);
	}
	void Delete(int p)
	{
		if (p < rm.size())
		{
			rm.erase(rm.begin()+p);
		}
	}
	Room search(string t)
	{
		for (int i = 0; i < rm.size(); i++)
		{
			if (t == rm[i].get_type())
				return rm[i];
		}
		return Room(0, "Room Not found");
	}
	string list()
	{
		string s;
		for (int i = 0; i < rm.size(); i++)
		{
			s += "Room:";
			s += rm[i].get_type();
			s += "  Area:";
			ostringstream convert;
			convert << rm[i].get_area();
			s += convert.str();
			s += "\n";
		}
		return s;
	}
	double Area()
	{
		double t = 0;
		for (int i = 0; i < rm.size(); i++)
		{
			t += rm[i].get_area();
		}
		return t;
	}

	void WriteToFile(string n)
	{
		ofstream file;
		file.open (n+".txt");
		for (int i = 0; i < rm.size(); i++)
		{
			file << rm[i];
		}
		file.close();
	}
	Building ReadFromFile(string n)
	{
		Building b;
		Room r;
		string l;
		ifstream file;
		file.open (n+".txt");
		while (getline (file,l))
		{
			string tmpName;
			string tmpArea;
			// Area
			for(int i = 0; i < l.length(); i++)
			{
				if (l[i] == 'A' && l[i+1] == 'r' && l[i+2] == 'e' && l[i+3] == 'a' && l[i+4] == ':')
				{
					i+=5;	//skip the positions with the value "Type:"
					while (l[i] != ' ')
					{
						tmpArea += l[i];
						i++;
					}
				}
			}
			// Name
			for(int i = 0; i < l.length(); i++)
			{
				if (l[i] == 'T' && l[i+1] == 'y' && l[i+2] == 'p' && l[i+3] == 'e' && l[i+4] == ':')
				{
					i+=5;	//skip the positions with the value "Type:"
					while (l[i] != ' ')
					{
						tmpName += l[i];
						i++;
					}
				}
			}
			double a;
			istringstream convert(tmpArea);
			convert >> a;
			r.set_area(a);
			r.set_type(tmpName);
			b.rm.push_back(r);
		}
		return b;
	}
};

